package com.sxbbc.plat.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.encrypts.des.DESEncode;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.SysMenus;
import com.sxbbc.common.core.entity.jz.SysRoles;
import com.sxbbc.common.core.entity.jz.SysUser;
import com.sxbbc.common.core.service.ISysMenusService;
import com.sxbbc.common.core.service.ISysRolesService;
import com.sxbbc.common.core.service.ISysUserService;
import com.sxbbc.plat.util.PlatSession;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author leehao
 * @description 平台登录-业务层
 * @date 2019-04-26 18:39
 */
@Service
public class PlatLoginService {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysMenusService sysMenusService;
    @Autowired
    private ISysRolesService sysRolesService;

    /**
     * 平台 去登录操作
     *
     * @param request   session
     * @param login     登录名
     * @param password  密码
     * @param validCode 验证码
     * @return 登录状态
     * @author leehao
     */
    public RestResponse  doLogin(HttpServletRequest request, String login, String password, String validCode) {
        String valid = PlatSession.getValidCode(request);
        CalcuUtils calcu = CalcuUtils.getInstance();

        if (StringUtils.isBlank(login)) {
            return GetRest.getFail("请输入用户名");
        }
        if (StringUtils.isBlank(password)) {
            return GetRest.getFail("请输入密码");
        }
        //TODO 项目开发阶段注释登录验证操作
        if (StringUtils.isBlank(validCode)) {
            return GetRest.getFail("请输入验证码");
        }
        if (!StringUtils.equalsIgnoreCase(valid, validCode)) {
            return GetRest.getFail("验证码不正确");
        }
        SysUser sysUser = sysUserService.selectByLogin(login);
        if (sysUser == null) {
            return GetRest.getFail("该用户不存在");
        }
        if (!calcu.isEquals(sysUser.getStatus(), StaticUtils.STATUS_YES)) {
            return GetRest.getFail("该用户已被冻结");
        }
        String mdPass = DESEncode.getInstance().encrypt(password);
        if (!StringUtils.equals(sysUser.getPassword(), mdPass)) {
            return GetRest.getFail("密码不正确");
        }
        SysRoles role = sysRolesService.getById(sysUser.getRoleid());
        if (role == null) {
            return GetRest.getFail("用户所属角色不存在");
        }
        if (!calcu.isEquals(role.getStatus(),StaticUtils.STATUS_YES)) {
            return GetRest.getFail("用户所属角色已被冻结");
        }
        List<Integer> list = calcu.getIntegerList(role.getMenuIds().split(","));
        if (list.isEmpty() || list.size() < 1) {
            return GetRest.getFail("当前角色没有菜单权限");
        }
        QueryWrapper<SysMenus> wrapper = new QueryWrapper();
        wrapper.in("id",list);
        List<SysMenus> menusList = sysMenusService.queryMenusList(wrapper);
        PlatSession.setMenu(request,menusList);
        PlatSession.setMenuUrl(request, menusList.stream().map(SysMenus :: getUrl).collect(Collectors.toList()));
        PlatSession.setSysUser(request, sysUser);
        PlatSession.setUserId(request, sysUser.getId());
        PlatSession.setRoleId(request, sysUser.getRoleid());
        return GetRest.getSuccess("登录成功");
    }
}
